﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using JBeamLibrary.Tools;

namespace DriveBeamEditor.Controls.BeamNG.Forms
{
    public partial class TextInputForm : Form
    {
        #region Properties

        /// <summary>Gets or sets the key text</summary>
        public string KeyText
        {
            get { return kvcInput.Caption; }
            set { kvcInput.Caption = value; }
        }

        /// <summary>Gets or sets the input text</summary>
        public string InputText
        {
            get { return kvcInput.EditValue.ToStringOrEmpty(); }
            set { kvcInput.EditValue = value.ToStringOrEmpty(); }
        }

        /// <summary>Gets or sets the message to display when user tries to close form without entering a text</summary>
        public string MandatoryWarning
        {
            get;
            set;
        }

        /// <summary>Gets or sets whether the input text is mandatory</summary>
        public bool IsMandatory
        {
            get;
            set;
        }

        #endregion

        #region CTORs

        public TextInputForm()
        {
            InitializeComponent();
        }

        #endregion

        #region Events

        private void xbOk_Click(object sender, EventArgs e)
        {
            if (!Modal) Close();
        }

        private void TextInputForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            /* No text entered */
            if (IsMandatory && string.IsNullOrEmpty(InputText))
            {
                var msg = string.IsNullOrEmpty(MandatoryWarning) ? @"Please fill in the text box" : MandatoryWarning;
                MessageBox.Show(this, msg, @"No text entered", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                e.Cancel = true;
            }
        }

        #endregion

        #region Factories

        public static TextInputForm GetNew(string title, string key, bool mandatory, string input = "", string mandatoryWarning = "")
        {
            return new TextInputForm
            {
                Text = title,
                KeyText = key,
                IsMandatory = mandatory,
                InputText = input,
                MandatoryWarning = mandatoryWarning
            };
        }

        #endregion
    }
}
